package love.yuanmengrobot.qianyugarden.controller;

import love.yuanmengrobot.qianyugarden.dao.UserDao;
import love.yuanmengrobot.qianyugarden.entity.LoginLog;
import love.yuanmengrobot.qianyugarden.entity.User;
import love.yuanmengrobot.qianyugarden.service.LoginLogService;
import love.yuanmengrobot.qianyugarden.utils.DataGridView;
import love.yuanmengrobot.qianyugarden.utils.MD5;
import love.yuanmengrobot.qianyugarden.utils.ResultObj;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.UnsupportedEncodingException;
import java.security.NoSuchAlgorithmException;
import java.util.List;

import static love.yuanmengrobot.qianyugarden.utils.ResultObj.*;

/**
 * (LoginLog)表控制层
 *
 * @author huanghongtao
 * @since 2021-07-13 14:00:38
 */
@Controller
@RequestMapping("loginLog")
public class LoginLogController {
    /**
     * 服务对象
     */
    @Autowired
    LoginLogService loginLogService;



    /**
     *登录
     *
     */
    @PostMapping("/login")
    @ResponseBody
    public ResultObj Login(User user, HttpServletRequest request) throws UnsupportedEncodingException, NoSuchAlgorithmException {
        User this_user =  loginLogService.queryLogin(user);
        if (this_user==null ){//登录失败
            return LOGIN_ERROR;
        }else {//登录成功
            request.getSession().setAttribute("UserInfo",this_user);
            return LOGIN_SUCCESS;
        }
    }

    /**
     *注册
     *
     *
     */
    @PostMapping("/signup")
    @ResponseBody
    public ResultObj Signup(User user){
        user.setHeadportrait("/img/userHeadportrait/userAvatar.jpg");
        int check_insert = this.loginLogService.insertUser(user);
        if (check_insert==3) {
            return SIGNUP_EXITT;
        }
        else if (check_insert==1){
            return SIGNUP_SUCCESS;
        }else return SIGNUP_ERROR;
    }


    @RequestMapping("/remove")
    public String remove(HttpSession session) {
        session.setAttribute("UserInfo", null);
        System.out.println("退出登录");
        return "index";
    }


    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @RequestMapping("selectOne")
    public LoginLog selectOne(String id) {
        return this.loginLogService.queryById(id);
    }

}
